package com.springboot.love.controller;

import com.alibaba.fastjson.JSONObject;
import com.springboot.love.dao.NoticeDao;
import com.springboot.love.entity.Notice;
import com.springboot.love.service.NoticeService;
import com.springboot.love.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Notice)控制层
 */
@Controller
@RequestMapping("notice")
@Slf4j
public class NoticeController {
    /**
     * 服务对象
     */
    @Resource
    private NoticeService noticeService;


    @RequestMapping("/noticeAll")
    public String noticeAll(){
        return "admin/listNotice";
    }

    /**
     * 查询所有公告
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/noticeList")
    @ResponseBody
    public String noticeAll(String page, String limit){
        PageUtil pageUtil=new PageUtil(Integer.parseInt(page),Integer.parseInt(limit));
        List<Notice> noticeList = noticeService.queryAllByLimit(pageUtil);
        int noticeTotal = noticeService.queryTotalByTime();
        JSONObject json = new JSONObject();
        json.put("code",0);
        json.put("data",noticeList);
        json.put("count", noticeTotal);
        json.put("status", 200);
        return json.toString();
    }

    /**
     * 添加公告
     * @return
     */

    @RequestMapping("/noticeadd")
    public String noticeadd(){
        return "admin/addNotice";
    }
    @RequestMapping("/addnotice")
    @ResponseBody
    public String addnotice(String noticeTitle,String noticeContent){
        return noticeService.insertNotice(noticeTitle,noticeContent);
    }

    /**
     * 更新公告
     * @param noticeId
     * @param noticeTitle
     * @param noticeContent
     * @return
     */
    @RequestMapping("/updatenotice")
    @ResponseBody
    public String updatenotice(String noticeId,String noticeTitle,String noticeContent){
        return noticeService.updateNotice(noticeId,noticeTitle,noticeContent);
    }

    /**
     * 单个删除公告
     * @param noticeId
     * @return
     */
    @RequestMapping("/deleteNotice")
    @ResponseBody
    public String deleteNotice(String noticeId){
        return noticeService.deleteByIdNotice(noticeId);
    }

    /**
     * 批量删除公告
     * @param delete
     * @return
     */
    @RequestMapping("/deleteNotices")
    @ResponseBody
    public String  deleteNotices(String delete){
        return noticeService.deletesNotice(delete);
    }


}